<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php include('menu.php'); ?>
</head>
<body>
<?php
include("../connect/connect.php");

$Query = "SELECT * FROM product,producttype WHERE product.prodtype_id = producttype.prodtype_id and prod_id = '".$_GET["id"]."'";
$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
$row = mysql_fetch_array($objQuery);

//แก้ไข
if($_GET["action"]=='update'){
	if($_POST['quantityadd'] != "")
		{
			$Select = "SELECT prod_qtysell FROM product WHERE prod_id = '".$_POST["product_id"]."' ";
			$Query = mysql_query($Select);
			$num = mysql_fetch_assoc($Query);
			
			if($num["prod_qtysell"] > $_POST['quantityadd']) {
			$quantity2 = $_POST["quantityadd"];
			$quantity1 = $_POST["quantity"];
			$quantity = $quantity1 - $quantity2;
			
			$Query = "UPDATE product SET prod_qtysell = '$quantity' WHERE prod_id = '".$_POST["product_id"]."' ";
			$objQuery = mysql_query($Query);
			
			echo "<script type=\"text/javascript\"> window.location=\"inventory_manage.php\"</script>";
			exit();
			} else {
			$save = "มีจำนวนสินค้าไม่เพียงพอ กรุณากรอกสินค้าที่ต้องการเบิกใหม่";
			}
		} else {
		$save = "กรุณาระบุจำนวนสินค้าที่ต้องการเบิก";
		}
}
?>
<form action="?action=update&amp;id=<?php echo $_GET["id"]?>" name="formedit" method="post" id="formedit">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading">
      <h1>เบิกสินค้า</h1>
      <div class="buttons"><a onclick="formedit.submit();" class="button"><span>บันทึก</span></a><a onclick="location = 'inventory_manage.php';" class="button"><span>ยกเลิก</span></a></div></div>
  <div class="content">
    <table class="form">
      <tr>
        <td>รหัสสินค้า : </td>
        <td><input name="code" type="text" readonly="readonly" id="code" value="<?php echo $row["prod_barcode"];?>" />
		<input name="product_id" type="hidden" id="product_id" value="<?php echo $row["prod_id"];?>" /></td>
      </tr>
      <tr>
        <td>ชื่อสินค้า : </td>
        <td><input name="name" type="text" readonly="readonly" id="name" value="<?php echo $row["prod_name"];?>" /></td>
      </tr>
      <tr>
        <td>หมวดสินค้า : </td>
        <td><input name="category_id" type="text" readonly="readonly" id="category_id" value="<?php echo $row["prodtype_name"];?>" /></td>
      </tr>
      <tr>
        <td>จำนวนที่มีอยู่เดิม : </td>
        <td><input name="quantity" type="text" readonly="readonly" id="quantity" value="<?php echo $row["prod_qtysell"];?>" /></td>
      </tr>
      <tr>
        <td>จำนวน : </td>
        <td><input name="quantityadd" type="text" id="quantityadd" /></td>
      </tr>
    </table>
	<?php echo "<br><font color=\"red\"><center>$save</center></font>"; ?>
  </div>
</div>
</form>
</body>
</html>
